using UnityEngine;
using System.Collections;

public class UIViewor : UIBas
{
		private UIViewor parent;
		private UIViewor child;

		public virtual bool doAppear ()
		{
				if (child == null) {
						viewDidAppear ();
						return true;
				}


				child.doAppear ();
				return false;
		}

		public virtual bool doDisappear ()
		{
				if (child == null) {
						viewDidDisappear ();
						return true;
				}
		
				child.doDisappear ();
				return false;
		}
	
		public virtual void viewDidAppear ()
		{
		
		}
	
		public virtual void viewDidDisappear ()
		{
		
		}
	
		public UIViewor getParent ()
		{
				return parent;
		}

		public void presentViewor (UIViewor viewor)
		{
				child = viewor;
				viewor.parent = this;
				Transform container = gameObject.transform.parent;
				GameObjectUtils.getGameObjectComponent<UIRetain> (gameObject).retain ();
				VieworUtils.doDisappear (this);
				VieworUtils.doAppear (viewor, container);
		}
	
		public void presentGameObject (GameObject go)
		{
				presentViewor (GameObjectUtils.getGameObjectComponent<UIViewor> (go));
		}
	
		public UIViewor dismissViewor ()
		{
				if (parent != null) {
						Transform container = gameObject.transform.parent; 
						VieworUtils.doDisappear (this);
						GameObjectUtils.getGameObjectComponent<UIRetain> (parent.gameObject).release ();
						parent.child = null;
						UIViewor viewor = parent;
						parent = null;
						VieworUtils.doAppear (viewor, container);
						return viewor;
				}

				return null;
		}
}
